package com.kongshidian.ui;

import java.lang.ref.WeakReference;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import com.kongshidian.App;
import com.kongshidian.Constants;
import com.kongshidian.R;
import com.kongshidian.UserBaseInfo;
import com.kongshidian.bean.ServiceResult;
import com.kongshidian.http.WebClient;
import com.kongshidian.http.WebException;
import com.kongshidian.ui.view.SimpleTitleBar;
import com.kongshidian.util.ContextUtil;
import com.kongshidian.util.PreferenceUtil;
import com.kongshidian.util.StringUtil;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 注册
 * @author fbl
 */
@SuppressLint("HandlerLeak") 
public class RegisterActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.register_cardname_edit) EditText mRegisterCardNameEdit;
	@ViewInject(id = R.id.register_cardno_edit) EditText mRegisterCardNoEdit;
	@ViewInject(id = R.id.registernext_but , click = "butClick") Button mRegisterNextBut;



	public static RegisterActivity instance = null;
	private String mUniqueId;
	private Handler mHandler = new MyHandler(this);

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_register);
		FinalActivity.initInjectedView(this);
		instance = this;

		mUniqueId = getMyUUID();


		setEditFocusChangeLister();
	}

	private void setEditFocusChangeLister() {

		/*监听用户名EditText失去焦点的时候判断用户名是否符合规定 */
		mRegisterCardNameEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					if(invalidateText(mRegisterCardNameEdit)){
						mRegisterCardNameEdit.setError(getString(R.string.txt_info_card_name));
						return;
					}
				}
			}
		});

		mRegisterCardNoEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					if(invalidateText(mRegisterCardNoEdit)){
						mRegisterCardNoEdit.setError(getString(R.string.txt_info_card_no));
						return;
					}
					//验证是不是数字
					if( StringUtil.regxStr(getText(mRegisterCardNoEdit), "^[0-9]*$")){
						mRegisterCardNoEdit.setError(getString(R.string.txt_info_true_card_no));
						return;
					}
				}
			}
		});

	}

	public void butClick(View v){
		//
		if( v == mRegisterNextBut){
			doNextJudge();
		}
	}



	/* 先检测用户名是否已经存在 */
	private void checkCard() {

		new Thread(){
			public void run() {
				try {
					ServiceResult<String> ret = WebClient.getInstance(RegisterActivity.this).checkCard(getText(mRegisterCardNameEdit),getText(mRegisterCardNoEdit));
					mHandler.sendEmptyMessage(ret.getCode());
				} catch (WebException e) {
					mHandler.sendEmptyMessage(0); //通知卡名已存在
				}
			}
		}.start();

		
		
	}

	private void doNextJudge() {
		if(invalidateText(mRegisterCardNameEdit,R.string.txt_info_card_name)
				|| invalidateText(mRegisterCardNoEdit,R.string.txt_info_card_no)
				){
			App.showShortToast(getString(R.string.txt_info_true_card));
			return;
		}
		checkCard();
	}


	private class MyHandler extends Handler{

		private WeakReference<Activity> mActivity;

		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if( mActivity.get() != null ){

				switch (msg.what) {
				case 0:
					//mRegisterCardNameEdit.setError(getString(R.string.info_please_input_cardName_is_notexist));
					App.showShortToast(getString(R.string.request_failure));
					break;
				case 1:
					//注册成功
					App.showShortToast(getString(R.string.card_success));
					ContextUtil.startActivity(RegisterActivity.this, Register2Activity.class);
					//finish();
					break;
				case 2:
					//注册失败
					App.showShortToast(getString(R.string.txt_info_true_card));
					break;
				case 3:
					LoginActivity.instance.finish();
					break;

				default:
					break;
				}

			}

		}

	}

	@Override
	protected void onTitleBarCreated(SimpleTitleBar titleBar) {
		super.onTitleBarCreated(titleBar);
		titleBar.setTitle(R.string.title_register);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacksAndMessages(null);
	}


}
